package com.example.fourth;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ShareCompat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.example.fourth.databinding.ActivityMainBinding;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.btn1.setOnClickListener(v -> {
            gotoActivity();
        });
        binding.btn2.setOnClickListener(v -> {
            callPhone();
        });
        binding.btn3.setOnClickListener(v -> {
            sendMessage();
        });
        binding.btn4.setOnClickListener(v -> {
            openBrowser();
        });
        binding.btn5.setOnClickListener(v -> {
            shareText("分享");
        });





    }
    private void gotoActivity(){
        Intent intent = new Intent();
        intent.setAction("com.example.implicit.intent.START");
        intent.addCategory(Intent.CATEGORY_DEFAULT);

        intent.putExtra("data","软件2058");
        ArrayList<Integer> datas = new ArrayList<>();
        datas.add(85);
        datas.add(90);
        datas.add(78);
        intent.putIntegerArrayListExtra("list",datas);
        final Bundle bundle = new Bundle();
        bundle.putString("bundle1","软件2076");
        bundle.putIntegerArrayList("bundle2",datas);
        intent.putExtras(bundle);

        User user = new User("张三", 19);
        intent.putExtra("User", user);

        startActivity(intent);



    }
    private void callPhone(){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:10086"));
        startActivity(intent);
    }
    private void sendMessage(){
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("smsto:10086"));
        intent.putExtra("sms_body","10086,你好");
        startActivity(intent);
    }
    private void openBrowser(){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://www.baidu.com/"));
        startActivity(intent);
    }
    private void shareText(String shareText){
        final String txtMimeType = "text/plain";
        final String picMimeType = "image/*";
        final String chooser = "分享列表";

        ShareCompat.IntentBuilder.from(this)
                .setText(shareText)
                .setChooserTitle(chooser)
                .setType(txtMimeType)
                .setType(picMimeType)
                .startChooser();
    }


    private ActivityResultLauncher<String> launcher = registerForActivityResult(
            new ResultContract(),
            new ActivityResultCallback<String>() {
                @Override
                public void onActivityResult(String result) {
                    binding.btnObject.setText("返回的数据为："+result);
                    Log.i("MainActivity", "返回的数据为: "+result);
                }
            }


    );

    class ResultContract extends ActivityResultContract<String,String>{

        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, String input) {
            Intent intent = new Intent(MainActivity.this,SecondActivity.class);
            intent.putExtra("data",input);
            return intent;
        }

        @Override
        public String parseResult(int resultCode, @Nullable Intent intent) {
            if(resultCode== Activity.RESULT_OK&&intent!=null){
                return intent.getStringExtra("data");
            }
            return null;
        }
    }
}